/**
 * @Title: wxController.java
 * @Description: 注解类
 * <p>
 * Copyright(C) 2018-2019    NPlus
 * Company:   无锡核心信息科技有限公司
 * @version V1.0
 * @Date: 11:08 2018/10/18
 * <p>
 * 修改历史:
 * Date                 Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 11:08 2018/10/18       zhangshuwei        1.0            初始化版本
 */
package com.imooc.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;


@Controller
@RequestMapping("/wx")
@Api(value = "/wx", tags = "微信测试接口模块")
@Slf4j
public class wxController {

    /**
     * 类目列表
     * @param
     * @return
     */
    @ApiOperation(value = "获取code", notes = "")
    @RequestMapping(value = "/auth", method = RequestMethod.GET)
    @ResponseBody
    public void  wxCode(@RequestParam("code") String code,@RequestParam("state") String state) {
        log.info("***进入获取code方法***");
        log.info("code={}",code);
        log.info("state={}",state);
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx2e6cc3ca61a1249c&secret=65ff50ed2b5f2068b3b1fcff2c6aa80c&code="+code+"&grant_type=authorization_code";
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        String result = restTemplate.getForObject(url, String.class);
        JSONObject json = JSONObject.parseObject(result);
        String openid = json.get("openid").toString();
        String access_token =json.get("access_token").toString();
        String urlInfo = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";
        String result1 = restTemplate.getForObject(urlInfo, String.class);
        log.info("result1={}",result1);

    }

}
